/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-09-28
 * Time: 11:26
 */
public class MyCircularQueue {
    private int[]elem;
    private int head;
    private int tail;
    public MyCircularQueue(int k) {
        //多开一个空间用来区分队列是空还是满
        this.elem=new int[k+1];
    }

    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }
        this.elem[tail]=value;
        //tail为length-1
        tail=(tail+1)%elem.length;
        return true;
    }

    private boolean isFull(){
        if((tail+1)%elem.length==head){
            return true;
        }
        return false;
    }
    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }
        head=(head+1)%elem.length;
        return true;
    }

    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return elem[head];
    }

    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        //注意tail为0的情况
        return elem[(tail+elem.length-1)%elem.length];
    }

    public boolean isEmpty() {
        return head==tail;
    }

}
